﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicLib
{
    public class GraphicSplit
    {
        /// <summary>
        /// 切割一次图像得到的像素点数组
        /// </summary>
        /// <param name="bytes">灰度化后的像素点数组</param>
        /// <param name="blockCount">切割成的块数</param>
        /// <returns></returns>
        public static List<byte[]> SingleSplitGraphicBytes(byte[] bytes, int blockCount)
        {
            List<byte[]> bytesList = new List<byte[]>();
            List<byte> byteList = bytes.ToList();
            int blockSize = byteList.Count / blockCount; //每块的大小
            for (int i = 0; i < blockCount; i++)
            {
                List<byte> blockByteList = new List<byte>();    //每块的byte列表
                int maxJ = blockSize;
                if (i == blockCount - 1) //最后一块的大小将取剩下所有的
                {
                    maxJ = byteList.Count;
                }
                for (int j = 0; j < blockSize; j++)
                {
                    blockByteList.Add(byteList[j]);
                }
                bytesList.Add(blockByteList.ToArray());
                byteList.RemoveRange(0, maxJ);
            }
            return bytesList;
        }

        /// <summary>
        /// 切割多次图像得到的像素点数组列表
        /// </summary>
        /// <param name="bytes">灰度化后的像素点数组</param>
        /// <param name="splitCount">切割次数</param>
        /// <returns>外层List表示数次切割的值，内层List表示每一次切割的值，byte[]表示每一块具体的值</returns>
        public static List<List<byte[]>> SplitGraphicBytes(byte[] bytes, int splitCount)
        {
            List<List<byte[]>> graphicBytesList = new List<List<byte[]>>();
            for (int i = 0; i < splitCount; i++)
            {
                graphicBytesList.Add(SingleSplitGraphicBytes(bytes, (int) Math.Pow(4, i + 1)));
            }
            return graphicBytesList;
        }


    }
}
